package com.ruoyi.common.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.ruoyi.common.constant.RedisConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

/**
 * 短信发送工具类
 *
 * @author chenjian
 */
@Component
public class ALSmsSendUtils {

    @Autowired
    private RedisCache redisCache;

    private static RedisCache staticRedisCache;

    @PostConstruct
    public void init() {
        staticRedisCache = redisCache;
    }

    /**密钥ID*/
    public static final String ACCESSKEYID="LTAI5t5uqMDPj8WVibyG1apq";
    /**密钥*/
    public static final String ACCESSKEYSECRET="AWTGLdgFoKZ1AJ9dBNijj5xvgqQwll";
    /**机房信息*/
    public static final String SMS_REGION_ID = "cn-hangzhou";
    /**短信接口地址*/
    public static final String SMS_DOMAIN ="dysmsapi.aliyuncs.com";
    /**短信接口版本号*/
    public static final String SMS_SYS_VERSION="2017-05-25";
    /**短信接口名称*/
    public static final String SMS_SYS_ACTION ="SendSms";

    /**
     * 发送手机验证码
     * @param phone 手机号
     * @author chenjian
     */
    public static void sendVerCode(String phone) {
        Long expireTime = staticRedisCache.getExpireTime(RedisConstants.SEND_VER_CODE+phone);

        if(expireTime>=840){
          throw new CustomException("请稍后再试");
        }

        String signName="般鹏网络";
        String templateCode="SMS_213385361";
        String code= ((int)((Math.random()*9+1)*1000))+"";

        IClientProfile profile = DefaultProfile.getProfile(SMS_REGION_ID, ACCESSKEYID, ACCESSKEYSECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(SMS_DOMAIN);
        request.setSysVersion(SMS_SYS_VERSION);
        request.setSysAction(SMS_SYS_ACTION);
        request.putQueryParameter("RegionId", SMS_REGION_ID);
        request.putQueryParameter("PhoneNumbers", phone);
        //管理控制台中配置的短信签名（状态必须是验证通过）
        request.putQueryParameter("SignName", signName);
        //System.out.println("======================="+signName);
        //管理控制台中配置的审核通过的短信模板的模板CODE（状态必须是验证通过）
        request.putQueryParameter("TemplateCode", templateCode);

        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        staticRedisCache.setCacheObject(RedisConstants.SEND_VER_CODE+phone, code, 15,TimeUnit.MINUTES);
    }

    /**
     * 发送修改手机号验证码
     * @param phone 手机号
     * @author chenjian
     */
    public static void sendUpdatePhoneCode(String phone) {
        Long expireTime = staticRedisCache.getExpireTime(RedisConstants.SEND_UPDATE_PHONE_CODE+phone);

        if(expireTime>=840){
            throw new CustomException("请稍后再试");
        }

        String signName="般鹏网络";
        String templateCode="SMS_213385359";
        String code= ((int)((Math.random()*9+1)*1000))+"";

        IClientProfile profile = DefaultProfile.getProfile(SMS_REGION_ID, ACCESSKEYID, ACCESSKEYSECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(SMS_DOMAIN);
        request.setSysVersion(SMS_SYS_VERSION);
        request.setSysAction(SMS_SYS_ACTION);
        request.putQueryParameter("RegionId", SMS_REGION_ID);
        request.putQueryParameter("PhoneNumbers", phone);
        //管理控制台中配置的短信签名（状态必须是验证通过）
        request.putQueryParameter("SignName", signName);
        //System.out.println("======================="+signName);
        //管理控制台中配置的审核通过的短信模板的模板CODE（状态必须是验证通过）
        request.putQueryParameter("TemplateCode", templateCode);

        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        staticRedisCache.setCacheObject(RedisConstants.SEND_UPDATE_PHONE_CODE+phone, code, 15,TimeUnit.MINUTES);
    }


    /**
     * 检查手机验证码是否正确
     *
     * @author chenjian
     */
    public static boolean checkVerCode(String phone,String inputVerCode) {
        String verCode=staticRedisCache.getCacheObject(RedisConstants.SEND_VER_CODE+phone);
        if(inputVerCode.equals(verCode)){
            return true;
        }
        return false;
    }

    /**
     * 检查修改手机号验证码是否正确
     *
     * @author chenjian
     */
    public static boolean checkUpdatePhoneCode(String phone,String inputVerCode) {
        String verCode=staticRedisCache.getCacheObject(RedisConstants.SEND_UPDATE_PHONE_CODE+phone);
        if(inputVerCode.equals(verCode)){
            return true;
        }
        return false;
    }

}
